<?php
namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\UserModel;
use app\common\model\CompanyModel;

/**  YSF
 *   客户列表 后台
 *   Date: 2019/4/19
 * Class Customer
 * @package app\admin\controller
 */
class Customer extends AdminController
{
    protected $channelModel;
    protected $promotersModel;
    protected $UserModel;
    protected $CompanyModel;

    // 初始化构造方法
    public function __construct()
    {
        parent::__construct();
        $this->UserModel=new UserModel();//用户模型
        $this->CompanyModel=new CompanyModel();//企业模型
    }
    //获取用户列表数据
    public function index(){
        if ($this->request->get('type') == 'ajax') {
            $page = $this->request->get('page', 1);
            $limit = $this->request->get('limit', 10);
            $select = (array)$this->request->get('select', []);

            // 搜索条件
            foreach ($select as $k => $v)
            {
                if ($k == 'key' && $v != '') {
                    $where['a.realname|a.telphone|a.account|a.idcard'] = $v;
                }
                if ($k == 'company_id' && $v != '') {
                    $where['a.company_id'] = $v;
                }
            }

            $join = [
                ['company c', 'c.company_id=a.company_id', 'left'],
            ];
            $where['a.is_deleted'] = 0;
            $data = $this->UserModel->getAll($where, 'a.*,c.company_name', $page, $limit, $join);
            foreach ($data as $k=>$v){
                $data[$k]['create_time']=$v['create_time']?date('Y-m-d H:i',$v['create_time']):'--';
                $data[$k]['sex']=$v['sex']?($v['sex']==1?'男':'女'):'未知';
            }

            $count = $this->UserModel->getCount($where, $join);

            // 数据组装
            $info = [
                'limit' => $limit,
                'page_current' => $page,
                'page_sum' => ceil($count / $limit),
            ];
            $msg = '暂无数据！';
            if ($data) {
                $msg = '查询成功！';
            }
            $list = [
                'code' => 0,
                'msg' => $msg,
                'count' => $count,
                'info' => $info,
                'data' => $data,
            ];
            return json($list);
        }

        // 获取所有企业
        $company = $this->CompanyModel->getAll([],'company_id,company_name');
        // 基础数据
        $basic_data = [
            'title' => '管理员列表',
            'data'  => '',
            'company' => $company,
        ];

        return $this->fetch('', $basic_data);
    }










}